//
//  CheckDetailViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/8.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CheckDetailViewController.h"
#import "CheckDetailTableViewCell.h"
@interface CheckDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *listArray;
@property (nonatomic, assign)int  pageNo;
@end

@implementation CheckDetailViewController


-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray=[NSMutableArray array];
    }
    return _listArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self showNav];
    UIImage* itemImage= [UIImage imageNamed:@"icon_message_nor"];
    itemImage = [itemImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:itemImage style:UIBarButtonItemStylePlain target:self action:@selector(showMessageClick)];
    self.navigationItem.rightBarButtonItem =rightBarButtonItem;

}
-(void)showMessageClick{
    EaseConversationListViewController *chatController = [[EaseConversationListViewController alloc]init];
    chatController.title=MyString(@"消息");
    [self.navigationController pushViewController:chatController animated:YES];
    //NSLog(@"消息");
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.title=MyString(@"明细");
    [self.view addSubview:[self tableView]];
    self.pageNo=1;
    [self loadcountDetailRequest];
    // Do any additional setup after loading the view.
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle=0;
        [_tableView registerNib:[UINib nibWithNibName:@"CheckDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CheckDetailTableViewCell"];
        _tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.pageNo++;
            [self loadcountDetailRequest];
            [_tableView.mj_footer endRefreshing];
        }];
       // _tableView.mj_header=[MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderClick)];
    }
    return _tableView;
}
-(void)refreshHeaderClick{
//    [self.listArray removeAllObjects];
//    self.pageNo=1;
//    //
//    [self loadcountDetailRequest];
//    [_tableView.mj_header endRefreshing];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CheckDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CheckDetailTableViewCell"];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"CheckDetailTableViewCell"];
    }
    cell.name.text=[NSString stringWithFormat:@"%@",self.listArray[indexPath.row][@"fundDirection"]];
    cell.time.text=[self timeWithTimeIntervalString:[NSString stringWithFormat:@"%@",self.listArray[indexPath.row][@"updatedAt"]]];
    cell.money.text=[NSString stringWithFormat:@"%@",self.listArray[indexPath.row][@"change"]];
    cell.balance.text=[NSString stringWithFormat:@"%@",self.listArray[indexPath.row][@"balance"]];
    return cell;
}
-(void)loadcountDetailRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"pageSize"]=@"10";
    [self doHttpRequestWithApi:BILLDETAIL method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self.listArray addObjectsFromArray:dic[@"data"][@"list"]];
        [self.tableView reloadData];
    }];
}
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

@end
